﻿using HandyControl.Controls;
using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace weighing
{
    public class LockData
    {
        private static LockData instance;

        public static LockData Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LockData();
                }
                return instance;
            }
        }

        private CompanyVo inlockSend;
        public CompanyVo InLockSend
        {
            get
            {
                if (inlockSend == null)
                {
                    inlockSend = LoadFromXml()?.inLockReceive;
                }
                return inlockSend;
            }
            set
            {
                inlockSend = value;
            }
        }

        private CompanyVo inLockReceive;
        public CompanyVo InLockReceive
        {
            get
            {
                if (inLockReceive == null)
                {
                    inLockReceive = LoadFromXml()?.inLockReceive;
                }
                return inLockReceive;
            }
            set { inLockReceive = value; }
        }
        private MaterialVo inLockMaterial;
        public MaterialVo InLockMaterial
        {
            get
            {
                if(inLockMaterial == null)
                {
                    inLockMaterial = LoadFromXml()?.inLockMaterial;
                }
                return inLockMaterial;
            }
            set { inLockMaterial = value; }
        }


        private CompanyVo outLockSend;
       public CompanyVo OutLockSend { 
            get {
                if (outLockSend == null)
                {
                    outLockSend = LoadFromXml()?.outLockSend;
                }
                return outLockSend;
            }
            set { outLockSend = value; }
        }
        private CompanyVo outLockReceive;
        public CompanyVo OutLockReceive {
            get
            {
                if (outLockReceive == null)
                {
                    outLockReceive = LoadFromXml()?.outLockReceive;
                }
                return outLockReceive;
            }
            set { outLockReceive = value; }
        }

        private MaterialVo outLockMaterial;
       public MaterialVo OutLockMaterial
        {
            get
            {
                if (outLockMaterial == null)
                {
                    outLockMaterial = LoadFromXml()?.outLockMaterial;
                }
                return outLockMaterial;
            }
            set { outLockMaterial = value; }
        }


        private LockData LoadFromXml()
        {
            String path = Constant.LockDataFile;
            if (FileHelper.Exists(path))
            {
                string xml = FileHelper.Reader(path, Encoding.UTF8);
                LockData data = (LockData)XmlHelper.Deserialize(typeof(LockData), xml);
                return data;
            }
            else
            {
                FileHelper.CreateFile(path);
                return null;
            }
        }
        /// <summary>
        /// 保存到文件
        /// </summary>
        /// <returns></returns>
        public void AsyncSaveToFile()
        {
            new Thread(() => {
                try
                {
                    String path = Constant.LockDataFile;
                    string xml = XmlHelper.Serialize(typeof(LockData), instance);
                    FileHelper.Write(path, xml);
                }
                catch (Exception e)
                {
                    var m = "保存锁定数据出错：" + e.Message;
                    Log.Error(m, e.StackTrace, true);
                }
            }).Start();
        }
    }
}
